package com.hpin.fansea.my;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.hpin.fansea.R;
import com.hpin.fansea.adapter.MyRoomNumberAdapter;
import com.hpin.fansea.base.BaseActivity;
import com.hpin.fansea.bean.BuildResult;
import com.hpin.fansea.utils.Constant;
import com.hpin.fansea.utils.HttpUtil;
import com.lidroid.xutils.http.RequestParams;

import java.util.List;

/**
 *
 *
 * Created by Aaron on 15/9/21.
 */
public class RoomActivity extends BaseActivity {

    private ListView listView;

    private String projectId;
    private String projectName;
    private String buildingNo;
    private String unitNo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_community_issue);

        projectId = getIntent().getStringExtra("projectId");
        projectName = getIntent().getStringExtra("projectName");
        buildingNo = getIntent().getStringExtra("buildingNo");
        unitNo = getIntent().getStringExtra("unitNo");

        initWidget();

        //获取房屋号
        getRoomNumFromNet();

    }

    private void initWidget() {

        listView = (ListView) findViewById(R.id.my_roomnumber_list);

        ImageView iv_left = (ImageView) findViewById(R.id.iv_left);
        TextView tv_title = (TextView) findViewById(R.id.tv_title);

        tv_title.setText(getResources().getString(R.string.room_num));

        iv_left.setOnClickListener(this);

    }

    /**
     * 获取房屋号
     */
    private void getRoomNumFromNet() {

        RequestParams params = new RequestParams();
        params.addQueryStringParameter("projectId", projectId);
        params.addQueryStringParameter("buildingNo", buildingNo);
        params.addQueryStringParameter("unitNo", unitNo);

        HttpUtil.getInstance().setGetRequest("获取中...", this, Constant.WEB_ROOT + Constant.COMMUNITROOM, params, new HttpUtil.SuccessListener() {
            @Override
            public void setOnSuccessListener(String str) {

                try {
                    BuildResult parseObject = JSONObject.parseObject(str, BuildResult.class);
                    if (Constant.STATUS_SUCESS.equals(parseObject.status)) {

                        initAdapter(parseObject.result);

                    } else {
                        showToast(parseObject.message);
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                    showToast("获取数据失败!");
                }


            }
        }, null);

    }

    private void initAdapter(final List<String> unitResult) {

        if (unitResult==null || unitResult.size() ==0)
            return;

        MyRoomNumberAdapter adapter = new MyRoomNumberAdapter(unitResult, this);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {

                SharedPreferences sp = getSharedPreferences(Constant.CONFIG, MODE_PRIVATE);
                SharedPreferences.Editor edit = sp.edit();

                edit.putString(Constant.communityId, projectId);//期次id
                edit.putString(Constant.projectName, projectName);//期次
                edit.putString(Constant.buildingNo, buildingNo);//楼号
                edit.putString(Constant.unitNo, unitNo);//单元号
                edit.putString(Constant.houseNo, unitResult.get(position));//房屋号

                //把当前认证状态改为认证中
                edit.putString(Constant.status , Constant.AUTH_WAIT);

                edit.commit();

                Intent intent = new Intent(RoomActivity.this, AuthActivity.class);
                startActivity(intent);
            }
        });

    }

    @Override
    public void widgetClick(View v) {

        switch (v.getId()) {

            case R.id.iv_left:

                finish();
                break;

        }

    }
}
